 package client;
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import handling.world.PlayerBuffValueHolder;
 import handling.world.WorldFindService;
 import java.util.ArrayList;
 import java.util.EnumMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import server.MapleStatEffect;
 import tools.Pair;

 public class MaplePlayerBuffManager {
/*  19 */   private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); private final String owner;
/*  20 */   private final ConcurrentHashMap<MapleBuffStat, MapleBuffEffect> effects = new ConcurrentHashMap<>();
/*  21 */   private final CopyOnWriteArrayList<MapleBuff> buffs = new CopyOnWriteArrayList<>();

   @JsonCreator
   public MaplePlayerBuffManager(@JsonProperty("owner") String owner) {
/*  25 */     this.owner = owner;
   }

   private MapleCharacter getPlayer() {
/*  29 */     return WorldFindService.getInstance().findCharacterByName(this.owner);
   }









   public void registerEffect(MapleStatEffect effect, long starttime, ScheduledFuture<?> schedule, List<Pair<MapleBuffStat, Integer>> stat) {
     try {
/*  42 */       this.lock.writeLock().lock();
/*  43 */       ArrayList<MapleBuffEffect> buffEffects = new ArrayList<>();
/*  44 */       MapleBuff buff = new MapleBuff(effect, starttime, schedule);
/*  45 */       for (Pair<MapleBuffStat, Integer> pair : stat) {
/*  46 */         MapleBuffEffect buffEffect = new MapleBuffEffect(buff, (MapleBuffStat)pair.getLeft(), (Integer)pair.getRight());
/*  47 */         this.effects.put(buffEffect.stat, buffEffect);
/*  48 */         buffEffects.add(buffEffect);
       }
/*  50 */       buffEffects.trimToSize();
/*  51 */       buff.buffEffects = buffEffects;
/*  52 */       this.buffs.add(buff);
     } finally {
/*  54 */       this.lock.writeLock().unlock();
     }
   }

   public boolean isActiveBuffedValue(int skillid) {
     try {
/*  60 */       this.lock.readLock().lock();
/*  61 */       for (MapleBuffEffect be : this.effects.values()) {
/*  62 */         if (be.getBuff().getEffect().isSkill() && be.getBuff().getEffect().getSourceid() == skillid) {
/*  63 */           return true;
         }
       }
/*  66 */       return false;
     } finally {
/*  68 */       this.lock.readLock().unlock();
     }
   }

   public Integer getBuffedValue(MapleBuffStat effect) {
/*  73 */     if (effect == null) {
/*  74 */       return null;
     }
     try {
/*  77 */       this.lock.readLock().lock();
/*  78 */       return this.effects.containsKey(effect) ? (this.effects.get(effect)).value : null;
     } finally {

/*  81 */       this.lock.readLock().unlock();
     }
   }

   public boolean isBuffFrom(MapleBuffStat stat, int skill) {
/*  86 */     if (stat == null) {
/*  87 */       return false;
     }
     try {
/*  90 */       this.lock.readLock().lock();
/*  91 */       return (this.effects.containsKey(stat) && ((MapleBuffEffect)this.effects.get(stat)).getBuff().getEffect().isSkill() && ((MapleBuffEffect)this.effects.get(stat)).getBuff().getEffect().getSourceid() == skill);
     } finally {
/*  93 */       this.lock.readLock().unlock();
     }
   }

   public int getBuffSource(MapleBuffStat stat) {
/*  98 */     if (stat == null) {
/*  99 */       return -1;
     }
     try {
/* 102 */       this.lock.readLock().lock();
/* 103 */       return this.effects.containsKey(stat) ? ((MapleBuffEffect)this.effects.get(stat)).getBuff()
/* 104 */         .getEffect().getSourceid() : -1;
     } finally {
/* 106 */       this.lock.readLock().unlock();
     }
   }

   public List<MapleStatEffect> getBuffEffects() {
     try {
/* 112 */       this.lock.readLock().lock();
/* 113 */       ArrayList<MapleStatEffect> almseret = new ArrayList<>();
/* 114 */       HashSet<Integer> hs = new HashSet<>();
/* 115 */       for (MapleBuffEffect buffEffect : this.effects.values()) {
/* 116 */         if (buffEffect != null && buffEffect.getEffect() != null) {
/* 117 */           Integer nid = Integer.valueOf(buffEffect.getEffect().isSkill() ? buffEffect.getEffect().getSourceid() : -buffEffect.getEffect().getSourceid());
/* 118 */           if (!hs.contains(nid)) {
/* 119 */             almseret.add(buffEffect.getEffect());
/* 120 */             hs.add(nid);
           }
         }
       }
/* 124 */       return almseret;
     } finally {
/* 126 */       this.lock.readLock().unlock();
     }
   }

   public void setBuffedValue(MapleBuffStat effect, int value) {
     try {
/* 132 */       this.lock.readLock().lock();
/* 133 */       MapleBuffEffect effect1 = this.effects.get(effect);
/* 134 */       if (effect1 != null) {
/* 135 */         effect1.value = Integer.valueOf(value);
       }
     } finally {
/* 138 */       this.lock.readLock().unlock();
     }
   }

   public Long getBuffedStarttime(MapleBuffStat effect) {
/* 143 */     return this.effects.containsKey(effect) ? Long.valueOf((((MapleBuffEffect)this.effects.get(effect)).getBuff()).startTime) : null;
   }

   public MapleStatEffect getStatForBuff(MapleBuffStat effect) {
/* 147 */     return this.effects.containsKey(effect) ? ((MapleBuffEffect)this.effects.get(effect)).getEffect() : null;
   }

   public List<MapleBuffStat> getBuffStats(MapleStatEffect effect, long startTime) {
     try {
/* 152 */       this.lock.readLock().lock();
/* 153 */       List<MapleBuffStat> stats = new ArrayList<>();
/* 154 */       for (MapleBuff buff : this.buffs) {
/* 155 */         if (buff.getEffect().sameSource(effect) && (startTime == -1L || buff.startTime == startTime)) {
/* 156 */           for (MapleBuffEffect buffEffect : buff.buffEffects) {
/* 157 */             stats.add(buffEffect.stat);
           }
           break;
         }
       }
/* 162 */       return stats;
     } finally {
/* 164 */       this.lock.readLock().unlock();
     }
   }

   public MapleStatEffect deregisterBuffStats(MapleBuff buff) {
/* 169 */     return deregisterBuffStats(buff, null);
   }








   public MapleStatEffect deregisterBuffStats(MapleBuff buff, List<MapleBuffStat> stats) {
/* 180 */     if (buff == null) {
/* 181 */       return null;
     }
     try {
/* 184 */       this.lock.writeLock().lock();
/* 185 */       for (MapleBuffEffect buffEffect : buff.buffEffects) {
/* 186 */         if (stats != null) {
/* 187 */           stats.add(buffEffect.stat);
         }
/* 189 */         this.effects.remove(buffEffect.stat);
       }
/* 191 */       if (buff.scheduledFuture != null) {
/* 192 */         buff.scheduledFuture.cancel(false);
/* 193 */         buff.scheduledFuture = null;
       }
/* 195 */       this.buffs.remove(buff);
/* 196 */       return buff.effect;
     } finally {
/* 198 */       this.lock.writeLock().unlock();
     }
   }

   public MapleBuff getBuff(MapleBuffStat stat) {
     try {
/* 204 */       this.lock.readLock().lock();
/* 205 */       MapleBuff buff = null;
/* 206 */       MapleBuffEffect buffEffect = this.effects.get(stat);
/* 207 */       if (buffEffect != null) {
/* 208 */         buff = buffEffect.getBuff();
       }
/* 210 */       return buff;
     } finally {
/* 212 */       this.lock.readLock().unlock();
     }
   }

   public MapleBuff getBuff(int skillId) {
     try {
/* 218 */       this.lock.readLock().lock();
/* 219 */       for (MapleBuff buff : this.buffs) {
/* 220 */         if (buff.getId() == skillId) {
/* 221 */           return buff;
         }
       }
/* 224 */       return null;
     } finally {
/* 226 */       this.lock.readLock().unlock();
     }
   }

   public MapleBuff getBuff(MapleStatEffect effect) {
     try {
/* 232 */       this.lock.readLock().lock();
/* 233 */       MapleBuff buff = null;
/* 234 */       for (MapleBuff mb : this.buffs) {
/* 235 */         if (mb.effect.sameSource(effect)) {
/* 236 */           buff = mb;
           break;
         }
       }
/* 240 */       return buff;
     } finally {
/* 242 */       this.lock.readLock().unlock();
     }
   }






   public List<PlayerBuffValueHolder> getAllBuffs() {
     try {
/* 253 */       this.lock.readLock().lock();
/* 254 */       List<PlayerBuffValueHolder> ret = new ArrayList<>();
/* 255 */       for (MapleBuff buff : this.buffs) {
/* 256 */         Map<MapleBuffStat, Integer> stat = new EnumMap<>(MapleBuffStat.class);
/* 257 */         for (MapleBuffEffect buffEffect : buff.buffEffects) {
/* 258 */           stat.put(buffEffect.getStat(), buffEffect.getValue());
         }
/* 260 */         ret.add(new PlayerBuffValueHolder(buff.getStartTime(), buff.getEffect(), stat));
       }
/* 262 */       return ret;
     } finally {
/* 264 */       this.lock.readLock().unlock();
     }
   }




   public void cancelMagicDoor() {
/* 272 */     for (MapleBuff buff : this.buffs) {
/* 273 */       if (buff.getEffect().is时空门()) {
/* 274 */         cancelEffect(buff);
       }
     }
   }




   public void dispel() {
/* 283 */     for (MapleBuff buff : this.buffs) {
/* 284 */       if (buff.getEffect().isSkill()) {
/* 285 */         cancelEffect(buff);
       }
     }
   }




   public void cancelAllBuffs() {
/* 294 */     for (MapleBuff buff : this.buffs) {
/* 295 */       cancelEffect(buff);
     }
   }




   public void cancelMorphs() {
/* 303 */     for (MapleBuff buff : this.buffs) {
/* 304 */       if (buff.getEffect().isMorph() && buff
/* 305 */         .getEffect().getSourceid() != 5111005 && buff
/* 306 */         .getEffect().getSourceid() != 5121003) {
/* 307 */         cancelEffect(buff);
       }
     }
   }







   public boolean hasBuff(int skillId) {
/* 319 */     for (MapleBuff buff : this.buffs) {
/* 320 */       if (buff.getEffect().getSourceid() == skillId) {
/* 321 */         return true;
       }
     }
/* 324 */     return false;
   }






   public void dispelSkill(int skillid) {
/* 333 */     for (MapleBuff buff : this.buffs) {
/* 334 */       if (skillid == 0) {
/* 335 */         if (buff.effect.isSkill() && buff.effect.getSourceid() % 20000000 == 1004)
/* 336 */           cancelEffect(buff);  continue;
       }
/* 338 */       if (buff.effect.isSkill() && buff
/* 339 */         .effect.getSourceid() == skillid) {
/* 340 */         cancelEffect(buff);
       }
     }
   }

   public void cancelEffect(MapleBuff buff) {
/* 346 */     cancelEffect(buff, false);
   }

   public void cancelEffect(MapleBuff buff, boolean overwrite) {
/* 350 */     if (getPlayer() != null) {
/* 351 */       getPlayer().cancelEffect(buff.getEffect(), overwrite, buff.getStartTime());
     }
   }

   public List<MapleBuffStat> getSpawnList(Map<MapleBuffStat, Integer> list) {
/* 356 */     ArrayList<MapleBuffStat> _buffs = new ArrayList<>();
/* 357 */     for (MapleBuffStat buffStat : list.keySet()) {
/* 358 */       if (this.effects.containsKey(buffStat) && ((MapleBuffEffect)this.effects.get(buffStat)).getEffect().isRefreshstyle()) {
/* 359 */         _buffs.add(buffStat);
       }
     }
/* 362 */     return _buffs;
   }

   public boolean hasBufferStat(MapleBuffStat stat) {
/* 366 */     return this.effects.containsKey(stat);
   }

   public boolean buffIsEmpty() {
/* 370 */     return this.buffs.isEmpty();
   }


   public String toString() {
/* 375 */     StringBuilder sb = new StringBuilder();
/* 376 */     String playerName = "";
/* 377 */     if (getPlayer() != null) {
/* 378 */       playerName = getPlayer().getName();
     }
/* 380 */     sb.append("技能储存对象：").append(getClass().getName()).append("\r\n");
/* 381 */     sb.append("玩家名字：");
/* 382 */     sb.append(playerName);
/* 383 */     sb.append("\r\n");
/* 384 */     sb.append("应用技能：\r\n");
/* 385 */     sb.append("BuffCount:").append(this.buffs.size()).append("\r\n");
     try {
/* 387 */       this.lock.readLock().lock();
/* 388 */       for (MapleBuff buff : this.buffs) {
/* 389 */         sb.append("Buff Id:").append(buff.getId()).append("\r\n");
/* 390 */         sb.append("StartTime:").append(buff.getStartTime()).append("\r\n");
/* 391 */         sb.append("ScheduledFuture:").append(buff.getScheduledFuture()).append("\r\n");
/* 392 */         for (MapleBuffEffect buffEffect : buff.buffEffects) {
/* 393 */           sb.append("Stat :").append(buffEffect.stat.toString()).append(" - Value:").append(buffEffect.value).append("\r\n");
         }
       }
     } finally {
/* 397 */       this.lock.readLock().unlock();
     }
/* 399 */     sb.append("Effects :").append(this.effects.size()).append("\r\n");
     try {
/* 401 */       this.lock.readLock().lock();
/* 402 */       for (MapleBuffEffect buffStat : this.effects.values()) {
/* 403 */         sb.append("From Buff:").append(buffStat.getBuff()).append("\r\n");
/* 404 */         sb.append("Stat:").append(buffStat.getStat()).append("-");
/* 405 */         sb.append("Value:").append(buffStat.getValue()).append("\r\n");
       }
     } finally {
/* 408 */       this.lock.readLock().unlock();
     }
/* 410 */     return sb.toString();
   }

   public void cancelSkill(int skill) {
/* 414 */     for (MapleBuff buff : this.buffs) {
/* 415 */       if (buff.getId() == skill) {
/* 416 */         if (getPlayer() != null) {
/* 417 */           getPlayer().cancelEffect(buff.getEffect(), false, buff.getStartTime());
         }
         break;
       }
     }
   }

   public void silentRemoveSkill(int skill) {
/* 425 */     for (MapleBuff buff : this.buffs) {
/* 426 */       if (buff.getId() == skill) {
/* 427 */         if (getPlayer() != null) {
/* 428 */           deregisterBuffStats(buff);
         }
         break;
       }
     }
   }


   public static class MapleBuff
   {
     private final MapleStatEffect effect;
     private final long startTime;
     private ScheduledFuture scheduledFuture;
     private List<MaplePlayerBuffManager.MapleBuffEffect> buffEffects;

     public MapleBuff(MapleStatEffect effect, long startTime, ScheduledFuture scheduledFuture) {
/* 444 */       this.effect = effect;
/* 445 */       this.startTime = startTime;
/* 446 */       this.scheduledFuture = scheduledFuture;
     }

     public int getId() {
/* 450 */       return getEffect().getSourceid();
     }

     public MapleStatEffect getEffect() {
/* 454 */       return this.effect;
     }

     public long getStartTime() {
/* 458 */       return this.startTime;
     }

     public ScheduledFuture getScheduledFuture() {
/* 462 */       return this.scheduledFuture;
     }


     public String toString() {
/* 467 */       return "ID :" + getId() + ";StartTime:" + getStartTime() + ";Schedule:" + getScheduledFuture();
     }
   }

   public static class MapleBuffEffect
   {
     private final MaplePlayerBuffManager.MapleBuff buff;
     private MapleBuffStat stat;
     private Integer value;

     public MapleBuffEffect(MaplePlayerBuffManager.MapleBuff buff, Integer value) {
/* 478 */       this.buff = buff;
/* 479 */       this.value = value;
     }

     public MapleBuffEffect(MaplePlayerBuffManager.MapleBuff buff, MapleBuffStat stat, Integer value) {
/* 483 */       this.buff = buff;
/* 484 */       this.stat = stat;
/* 485 */       this.value = value;
     }

     public MaplePlayerBuffManager.MapleBuff getBuff() {
/* 489 */       return this.buff;
     }

     public MapleStatEffect getEffect() {
/* 493 */       return getBuff().getEffect();
     }

     public MapleBuffStat getStat() {
/* 497 */       return this.stat;
     }

     public Integer getValue() {
/* 501 */       return this.value;
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\client\MaplePlayerBuffManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */